package com.liujit.gateway.route.controller;

import cn.hutool.json.JSONObject;
import com.liujit.common.domain.result.Result;
import com.liujit.gateway.route.domain.GatewayRouteDefinition;
import com.liujit.gateway.route.service.DynamicRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @description: 路由管理接口
 * @author: liujun
 * @createTime: 2021/6/9 9:44 上午
 **/
@RestController
@RequestMapping("/route")
@Api(value = "路由管理", tags = "路由管理")
public class RouteController {

    @Resource
    private DynamicRouteService dynamicRouteService;

    @GetMapping("/getList")
    @ApiOperation(value = "查询路由", notes = "查询全部路由信息")
    public Mono<Result> getList() {
        return dynamicRouteService.getList().map(tuple -> Result.success(tuple));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加路由", notes = "根据请求参数添加路由")
    public Mono<Result> add(@RequestBody GatewayRouteDefinition gatewayRouteDefinition) {
        return Mono.just(Result.success(dynamicRouteService.add(gatewayRouteDefinition)));
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除路由", notes = "根据id，删除路由")
    public Mono<Result> del(@RequestBody JSONObject params) {
        return Mono.just(Result.success(dynamicRouteService.del(params.getStr("id"))));
    }

    @PostMapping("/modify")
    @ApiOperation(value = "修改路由", notes = "根据请求参数修改路由")
    public Mono<Result> modify(@RequestBody GatewayRouteDefinition gatewayRouteDefinition) {
        return Mono.just(Result.success(dynamicRouteService.upd(gatewayRouteDefinition)));
    }

    @GetMapping("/get")
    @ApiOperation(value = "根据id查询路由", notes = "根据id查询路由")
    public Mono<Result> get(String id) {
        return dynamicRouteService.get(id).map(tuple -> Result.success(tuple));
    }

    @PostMapping("/refresh")
    @ApiOperation(value = "刷新路由", notes = "刷新路由")
    public Mono<Result> refresh() {
        return Mono.just(Result.success(dynamicRouteService.refresh()));
    }
}
